package cn.itcast.hotel;

import cn.itcast.hotel.pojo.Hotel;
import cn.itcast.hotel.pojo.HotelDoc;
import cn.itcast.hotel.service.IHotelService;
import com.alibaba.fastjson.JSON;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;
import java.util.List;

@SpringBootTest
public class EsDocumentDemo {

    @Autowired
    IHotelService iHotelService;

    @Test
    public void addDocument() throws IOException {

        Hotel hotel = iHotelService.getById(36934);

        HotelDoc hotelDoc = new HotelDoc(hotel);

        IndexRequest request = new IndexRequest("hotel");

        request.id(hotelDoc.getId() + "");

        String jsonStr = JSON.toJSONString(hotelDoc);

        request.source(jsonStr, XContentType.JSON);

        IndexResponse response = client.index(request, RequestOptions.DEFAULT);

        System.out.println(response.getResult());


    }

    @Test
    public void getDocumentById() throws IOException {

        GetRequest request = new GetRequest("hotel", "36934");
        // 2.发送请求，得到响应
        GetResponse response = client.get(request, RequestOptions.DEFAULT);
        // 3.解析响应结果
        String json = response.getSourceAsString();

        HotelDoc hotelDoc = JSON.parseObject(json, HotelDoc.class);
        System.out.println(hotelDoc);

    }

    @Test
    public void updateById() throws IOException {

        UpdateRequest request = new UpdateRequest("hotel", "36934");
        // 2.准备请求参数
        request.doc(
                "price", "952"
        );
        // 3.发送请求
        UpdateResponse response = client.update(request, RequestOptions.DEFAULT);

        System.out.println(response.getResult());

    }

    @Test
    public void deleteById() throws IOException {

        DeleteRequest request = new DeleteRequest("hotel", "36934");
        // 2.发送请求
        DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);

        System.out.println(response.getResult());

    }

    @Test
    public void testBulkRequest() throws IOException {
        // 批量查询酒店数据
        List<Hotel> list = iHotelService.list();

        // 1.创建Request
        BulkRequest bulkrequest = new BulkRequest("hotel");
        // 2.准备参数，添加多个新增的Request
        for (Hotel hotel : list) {
            IndexRequest request = new IndexRequest();
            request.id(hotel.getId() + "");
            HotelDoc hotelDoc = new HotelDoc(hotel);
            request.source(JSON.toJSONString(hotelDoc),XContentType.JSON);
            bulkrequest.add(request);
        }
        // 3.发送请求
        BulkResponse bulk = client.bulk(bulkrequest, RequestOptions.DEFAULT);
        System.out.println(bulk.status());
    }



    RestHighLevelClient client;
    @BeforeEach
    public void init(){
        client = new RestHighLevelClient(RestClient.builder(
                HttpHost.create("http://192.168.200.130:9200")
        ));
    }

    @AfterEach
    public void close(){
        try {
            client.close();
        } catch (IOException e) {

        }
    }
}
